package ssm.dao.impl;

import ssm.dao.Search;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SingletonSearch implements Search {
    /**
     * 单例模式
     * @param conn
     * @param rs
     * @param rowsNumber
     * @throws SQLException
     */
    @Override
    public void check(Connection conn, ResultSet rs, int rowsNumber) throws SQLException {
        int count=0;
        for (int i = 1; i <= rowsNumber; i++) {
            rs.absolute(i);    //将光标移动到此 ResultSet 对象的给定行编号
            String relationType = rs.getString("RelationType");
            if (relationType.equalsIgnoreCase("ASSOCIATION")) { //equalsIgnoreCase() 将此 String 与另一个 String 比较，不考虑大小写
                String s1 = rs.getString("SourceClass");
                String d1 = rs.getString("DestinationClass");
                //System.out.println("SourceClass:"+s1);
                //System.out.println("DestinationClass:"+d1);
                if (s1.equalsIgnoreCase(d1)) {
                    String sql2 = "INSERT INTO designs.singleton_instances(Singleton) VALUES (?)";
                    PreparedStatement stmt2 = conn.prepareStatement(sql2);

                    stmt2.setString(1, s1);

                    int rows2 = stmt2.executeUpdate();
                    count+=rows2;
                }
            }
        }
        System.out.println("found " + count + " Singleton");
    }
}
